import torch
import numpy as np
import skimage
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
torch.manual_seed(1)  # reproducible
import os
import pandas as pd
from PIL import Image
import warnings
warnings.filterwarnings('ignore')

transform = transforms.Compose([
    transforms.ToTensor(),  # 将图片转换为Tensor,归一化至[0,1]
])

class ClinicalDataProcessor:
    def __init__(self, clinical_csv_data):
        # 尝试不同的编码格式读取CSV文件
        encodings = ['utf-8', 'gbk', 'gb2312', 'latin1', 'cp1252']
        self.clinical_csv_data = None
        
        for encoding in encodings:
            try:
                self.clinical_csv_data = pd.read_csv(clinical_csv_data, encoding=encoding)
                print(f"成功使用 {encoding} 编码读取文件")
                break
            except UnicodeDecodeError:
                continue
            except Exception as e:
                print(f"使用 {encoding} 编码时出现其他错误: {e}")
                continue
        
        if self.clinical_csv_data is None:
            raise ValueError("无法使用任何编码格式读取CSV文件")
        
        self.processed_clinical_data = self._processed_clinical_data()
        
        # 确保输出目录存在
        os.makedirs('./preprocessed', exist_ok=True)
        self.processed_clinical_data.to_csv('./preprocessed/clinical_data.csv', index=False, encoding='utf-8')

    def _processed_clinical_data(self):
        """预处理临床数据集"""
        clinical_data = self.clinical_csv_data.copy()
        
        # 删除不需要的列
        columns_to_drop = [
            'Patient Name', 'Patient Birth Date', 'Ethnic Group', 'Study ID', 
            'Admitting Diagnosis Description', 'Study Instance UID', 'Series Instance UID',
            'Protocol Name', 'Manufacturer',
            'Manufacturer Model Name', 'Software Versions', 'Max Submission Timestamp', 
            'File Size', 'Collection URI', 'Date Released', 'License Name', 
            'License URI', 'Phantom', 'Species Code', 'Species Description', 
            'Annotations Flag', 'Third Party Analysis', 'Longitudinal Temporal Event Type', 
            'Project', 'Longitudinal Temporal Offset From Event', 'Series Date'
        ]
        
        # 可选：删除UID字段（如果不需要的话）
        # 取消注释下面的行来删除UID字段
        # columns_to_drop.extend(['Study Instance UID', 'Series Instance UID'])
        
        # 只删除存在的列
        existing_columns = [col for col in columns_to_drop if col in clinical_data.columns]
        clinical_data.drop(columns=existing_columns, inplace=True)
        
        print(f"预处理完成，保留列: {list(clinical_data.columns)}")
        print(f"数据形状: {clinical_data.shape}")
        
        return clinical_data
    
    def get_processed_data(self):
        """获取处理后的数据"""
        return self.processed_clinical_data
    
    def get_feature_columns(self):
        """获取特征列名"""
        return list(self.processed_clinical_data.columns)


def main():
    """主函数"""
    try:
        # 使用正确的文件路径
        clinical_csv_data = './raw/TCIA-CPTAC-CCRCC_v11_20230818-nbia-digest.csv'
        
        print("开始处理临床数据...")
        clinical_data_processor = ClinicalDataProcessor(clinical_csv_data)
        
        # 获取处理后的数据
        processed_data = clinical_data_processor.get_processed_data()
        print(f"处理后的数据形状: {processed_data.shape}")
        
        # 显示前几行数据
        print("\n处理后的数据预览:")
        print(processed_data.head())
        
        # 显示特征列
        feature_columns = clinical_data_processor.get_feature_columns()
        print(f"\n特征列: {feature_columns}")
        
        print("\n临床数据处理完成!")
        
    except Exception as e:
        print(f"处理过程中出现错误: {e}")
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    main() 